package com.panshi.modules.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.panshi.po.Answer;
import com.panshi.vo.QueryAnswerVO;
import com.panshi.vo.UpdAnswerChooseVO;
import com.panshi.vo.UserMemberVO;
import com.panshi.modules.domain.*;
import com.panshi.modules.mapper.*;
import com.panshi.modules.service.QaAnswerService;
import com.panshi.modules.utils.RelativeDateFormat;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;

@Service
@Slf4j
public class QaAnswerServiceImpl extends ServiceImpl<QaAnswerMapper, QaAnswer> implements QaAnswerService {

    @Resource
    QaAnswerMapper qaAnswerMapper;
    @Resource
    UserMemberMapper userMemberMapper;
    @Resource
    QaAnswerUncheckMapper qaAnswerUncheckMapper;
    @Resource
    QaQuestionMapper qaQuestionMapper;

    @Autowired
    UserDynamic userDynamic;
    @Autowired
    UserNotice userNotice;


    @Override
    public List<Answer> selectPage(QueryAnswerVO queryAnswerVO) {
        List<QaAnswer> qaAnswers = qaAnswerMapper.selectPage(queryAnswerVO);//查询数据
        List<Answer> answer = getQaAnswerVO(qaAnswers);//转换对象
        return answer;
    }

    @Override
    public Integer queryCount(final QueryAnswerVO queryAnswerVO) {
        QueryWrapper<QaAnswer> queryWrapper = new QueryWrapper();
        if (queryAnswerVO.getQuestionId()!=null){
            queryWrapper.eq("question_id",queryAnswerVO.getQuestionId());//根据问题id进行查询
        }
        Integer count = qaAnswerMapper.selectCount(queryWrapper);//查询总记录数
        return count;
    }

    @Override
    public int updChoose(final UpdAnswerChooseVO updAnswerChooseVO) {
        UpdateWrapper<QaAnswer> upd = new UpdateWrapper<>();
        upd.eq("id",updAnswerChooseVO.getId());
        upd.set("status",updAnswerChooseVO.getStatus());
        int update = qaAnswerMapper.update(null, upd);
        return update;
    }


    @Override
    public int del(Long id) {
        if(id==0){
            return 0;
        }
        //查询一下回答表中的数据
        QueryWrapper<QaAnswer> qaAnswerQueryWrapper = new QueryWrapper<>();
        qaAnswerQueryWrapper.eq("id",id);
        QaAnswer qaAnswer = qaAnswerMapper.selectOne(qaAnswerQueryWrapper);

        if(StringUtils.isEmpty(qaAnswer)){
            return 0;
        }

        //需要把审核表对应的回答也进行逻辑删除，无法直接获取审核id
        UpdateWrapper<QaAnswerUncheck> qaAnswerUncheckUpdateWrapper = new UpdateWrapper<>();
        qaAnswerUncheckUpdateWrapper
                .eq("user_id",qaAnswer.getUserId())
                .eq("question_id",qaAnswer.getQuestionId())
                .setSql("deleted=0");
        qaAnswerUncheckMapper.update(null,qaAnswerUncheckUpdateWrapper);

        //修改用户的回答数量
        UpdateWrapper<UserMember> userMemberUpdateWrapper = new UpdateWrapper<>();
        userMemberUpdateWrapper
                .eq("id", qaAnswer.getUserId())
                .setSql(" answer_num = answer_num-1");
        userMemberMapper.update(null,userMemberUpdateWrapper);

        //修改问题的回答数
        UpdateWrapper<QaQuestion>  qaQuestionUpdateWrapper= new UpdateWrapper<>();
        qaQuestionUpdateWrapper
                .eq("id", qaAnswer.getQuestionId())
                .setSql(" answer_num = answer_num-1");
        qaQuestionMapper.update(null,qaQuestionUpdateWrapper);

        //逻辑删除回答
        return qaAnswerMapper.deleteById(qaAnswer.getId());
    }


    //QaAnswer 转 Answer
    public List<Answer> getQaAnswerVO(List<QaAnswer> qaAnswers){
        List<Answer> qaAnswerVOs = new ArrayList<>();
        if(qaAnswers == null){
            return null;
        }
        for (QaAnswer item:qaAnswers){
            Answer qaAnswerVO = new Answer();
            BeanUtils.copyProperties(item,qaAnswerVO);
            qaAnswerVO.setNickname(item.getUserMember().getNickname());
            UserMemberVO userMemberVO = new UserMemberVO();
            BeanUtils.copyProperties(item.getUserMember(),userMemberVO);
            qaAnswerVO.setUserMemberVO(userMemberVO);
            //时间转换
            String dateformat = new RelativeDateFormat().format(item.getPublishTime());
            qaAnswerVO.setPublicDate(dateformat);
            qaAnswerVOs.add(qaAnswerVO);
        }
        return qaAnswerVOs;
    }
}
